//
//  PFWindow.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/1.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

struct DIcon {
    var title:String!
    var image:String!
    var identifer:String!
    var tag:Int!
    init(title:String,image:String,identifer:String,tag:Int) {
        self.title = title
        self.image = image
        self.identifer = identifer
        self.tag = tag
    }
}

typealias ToolbarBlock = (NSToolbarItem)->()

class PFWindow: NSWindow {
    
    var selectIdentifier:NSToolbarItem.Identifier = NSToolbarItem.Identifier.init("Setting_bacis")
    var selectBarItem:NSToolbarItem?
    
    var selectBlock:ToolbarBlock?
    
    var icons:[DIcon] = [
        DIcon.init(title: "基本设置", image: "icon_setting_bacis", identifer: "Setting_bacis", tag: 0),
        DIcon.init(title: "更新", image: "icon_setting_up", identifer: "Setting_update", tag: 1)
    ]
    
     override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
        super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
        
        self.collectionBehavior = NSWindow.CollectionBehavior.fullScreenNone
        self.styleMask = [.titled,.closable]
        self.isMovableByWindowBackground = true
        self.toolbar = self.customToolbar
        if let selectBarItem = self.selectBarItem {
            self.toolbarItemAction(senber: selectBarItem)
        }
        
    }
    
    lazy var customToolbar: NSToolbar = {
        let tool = NSToolbar.init(identifier: "toolbar")
        tool.allowsExtensionItems = true
        tool.autosavesConfiguration = true
        tool.showsBaselineSeparator = false
        tool.displayMode = NSToolbar.DisplayMode.iconAndLabel
        tool.sizeMode = NSToolbar.SizeMode.regular
        tool.delegate = self
        return tool
    }()
    
    
}

//MARK:- 事件
extension PFWindow {
    
    @objc func toolbarItemAction(senber:NSToolbarItem)  {
        self.selectIdentifier = senber.itemIdentifier
        self.customToolbar.selectedItemIdentifier = senber.itemIdentifier
        self.title = senber.label
        if selectBlock != nil {
            self.selectBlock!(senber)
        }
    }
    
    func fist(identifier:String,dataList:[DIcon]) -> DIcon? {
        let identifiers = dataList.map { (icons) -> NSToolbarItem.Identifier in
            return NSToolbarItem.Identifier.init(icons.identifer)
        }
        guard let indx = identifiers.firstIndex(of: NSToolbarItem.Identifier.init(identifier)) else {
            return nil
        }
        let di = dataList[indx]
        return di
    }
}

extension PFWindow: NSToolbarDelegate{
    
    func toolbarItem() -> [NSToolbarItem.Identifier] {
        return [
            NSToolbarItem.Identifier.separator,
            NSToolbarItem.Identifier.init(self.icons[0].identifer),
            NSToolbarItem.Identifier.init(self.icons[1].identifer),
            NSToolbarItem.Identifier.separator
        ]
    }
    
    func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return self.toolbarItem().removingDuplicates()
    }
    
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return self.toolbarItem()
    }
    
    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        let toolbarItem = NSToolbarItem.init(itemIdentifier: itemIdentifier)
        let notIcon = self.fist(identifier: itemIdentifier.rawValue, dataList: self.icons)
        guard let icon = notIcon  else {
            return nil
        }
        toolbarItem.label = icon.title
        toolbarItem.paletteLabel = icon.title
        toolbarItem.image = NSImage.image(name:icon.image)
        toolbarItem.tag = icon.tag
        toolbarItem.minSize = CGSize.init(width: 60, height: 100)
        toolbarItem.target = self
        toolbarItem.action = #selector(toolbarItemAction(senber:))
        
        if self.selectIdentifier == itemIdentifier {
            self.selectBarItem = toolbarItem
        }
        return toolbarItem
    }
    
    /*
    func toolbarSelectableItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return [self.selectIdentifier]
    }
    */
    
    func toolbarDidRemoveItem(_ notification: Notification) {
        
    }
    
    func toolbarWillAddItem(_ notification: Notification) {
        
    }
}
